package org.syncsoft.overalt.gui.impl.window.shareFolder;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.syncsoft.overalt.common.exception.SystemException;
import org.syncsoft.overalt.core.api.Core;
import org.syncsoft.overalt.core.api.ICore;
import org.syncsoft.overalt.domain.community.entity.Community;
import org.syncsoft.overalt.domain.folder.entity.TypeShare;
import org.syncsoft.overalt.domain.tag.entity.Tag;
import org.syncsoft.overalt.domain.user.entity.User;
import org.syncsoft.overalt.gui.api.WindowContextValue;
import org.syncsoft.overalt.gui.api.window.AbstractWindowController;

/**
 * 
 * @author tptfc
 *
 */
public class ShareFolderWindowControll extends AbstractWindowController<ShareFolderWindow> {
	private String path;
	
	public ShareFolderWindowControll (String path) {
		setPath(path);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.AbstractWindowController#doCreateWindow()
	 */
	@Override
	protected ShareFolderWindow doCreateWindow() throws SystemException {
		ShareFolderWindow window = new ShareFolderWindow(this);
		
		Collection<Community> communities = getCore().getComunities();
		Collection<User> contacts = getCore().getContacts();
		
		
		for (Community community : communities) {
			Collection<User> members = getCore().getCommunityMembers(community.getCode());
			community.setMembers(members);
		}
		
		window.setCommunities(communities);
		window.setContacts(contacts);
		
		return window;
	}
	
	@Override
	protected WindowContextValue getUpdateWindowContextValue() throws SystemException {
		WindowContextValue context = new WindowContextValue();
		
		if(getCore().isFolderShared(this.path)) {
			
			TypeShare typeShare = getCore().getFolderTypeShare(this.path);
			
			if(typeShare == TypeShare.COMMUNITY_FILES) {
				
				Community community = new Community(getCore().getFolderCommunityId(this.path));
				
				boolean isMemberComunnity = false;
				
				Collection<User> selectedCommunityMembers = getCore().getFolderMembersCommunity(this.path);
				
				context.addValue("community", community);
				
				if(selectedCommunityMembers != null && selectedCommunityMembers.size() > 0) {
					isMemberComunnity = true;
				}
				
				context.addValue("isMemberCommunity", isMemberComunnity);

				context.addMultiValue("selectedCommunityMembers", selectedCommunityMembers);
				
			} else if (typeShare == TypeShare.MY_FILES) {
				
				boolean autoFillTitleDesc = getCore().getFolderAutoFillTitleDescription(this.path);
				
				context.addValue("autoTitleAndDescription", autoFillTitleDesc);
			} else if (typeShare == TypeShare.CONTACTS_FILE) {
				
				Collection<User> selectedContacts = getCore().getFolderContacts(this.path);
				
				context.addMultiValue("selectedContacts", selectedContacts);
			}
			
			Collection<Tag> tags = getCore().getFolderTags(this.path);
			
			context.addValue("typeShare", typeShare);
			
			//context.addValue("requiredTags", tags);

			context.addMultiValue("requiredTags", tags);
			
		}

		return context;
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.AbstractWindowController#doSubmit(org.syncsoft.overalt.gui.swing.api.ContextWindowValue)
	 */
	@Override
	protected void doSubmit(WindowContextValue context) throws SystemException {
		
		TypeShare typeShare = context.getValue("typeShare");
		String tagsString = context.getValue("tags");
		boolean autoSync = context.getValue("autoSync") != null;
		Set<Tag> tags = parseTags(tagsString);
		if(typeShare == TypeShare.MY_FILES) {
			boolean autoTitleAndDescription = context.getValue("autoTitleAndDescription") != null;
			Core.getInstance().shareFolderMyFiles(getPath(), autoTitleAndDescription, autoSync, null, tags);
			
		} else if (typeShare == TypeShare.CONTACTS_FILE) {
			Collection<Integer> contactsCodeColl = context.getValues("contacts");
			Set<Integer> contactsCode = null;
			if(contactsCodeColl != null && contactsCodeColl.size() > 0) {
				 contactsCode = new HashSet<Integer>();
				 contactsCode.addAll(contactsCodeColl);
			}
			Core.getInstance().shareFolderContactFiles(getPath(), contactsCode, autoSync, null, tags);
			
		} else if (typeShare == TypeShare.COMMUNITY_FILES){
			Community community = context.getValue("community");
			Integer communityCode = null;
			if (community != null) {
				communityCode = community.getCode();
			}
			Collection<Integer> membersCodeColl = context.getValues("communityMembers");
			Set<Integer> membersCode = null;
			boolean isMemberCommunity = false;
			
			try {
				isMemberCommunity = context.getValue("isMemberCommunity");
			} catch(NullPointerException e) {
				
			}
			
			if(isMemberCommunity && membersCodeColl != null && membersCodeColl.size() > 0) {
				membersCode = new HashSet<Integer>();
				membersCode.addAll(membersCodeColl);
			}			
			Core.getInstance().shareFolderCommunityFiles(getPath(), communityCode, membersCode, autoSync, null, tags);
		}
		//Core.getInstance().shareFolder(getPath(), typeShare, autoTitleAndDescription, new HashSet<Integer>(contactsCode), communityCode, new HashSet<Integer>(membersCode), autoSync, null, tags);
	}
	
	public static void main(String[] args) throws SystemException {
		ShareFolderWindowControll controll = new ShareFolderWindowControll("/home/tptfc/Developer/java/syncsoft/amigos-dropbox-fork");
		controll.start();
	}
	
	protected ICore getCore() {
		return Core.getInstance();
	}

	private String getPath() {
		return path;
	}
	private void setPath(String path) {
		this.path = path;
	}
}
